package com.bingyuan.aipassproject.view.widget;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bingyuan.aipassproject.R;
import com.bingyuan.aipassproject.utils.ToastUtil;

public abstract class ExchangeCoinPopwindow {

    private PopupWindow popupWindow;
    private Activity context;
    private int mNum = 1;
    private String user_score;
    private String lock_score;
    private String ad_balance;
    private int prp = 10;//一个AD兑换10个金币

    /**
     * 构造函数
     * @param context
     */
    public ExchangeCoinPopwindow(Activity context, String user_score, String lock_score, String ad_balance) {
        this.context = context;
        this.user_score = user_score;
        this.lock_score = lock_score;
        this.ad_balance = ad_balance;
    }


//    public ExchangeCoinPopwindow(Activity context) {
//        this.context = context;
//    }

    /**
     * 显示警示框
     */
    public void show() {
        View popView = View.inflate(context, R.layout.pop_exchange_coin, null);
        popupWindow = new PopupWindow(context);
        popupWindow.setAnimationStyle(R.style.popWindow_animation);//设置动画
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(null);
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        popupWindow.setWidth(width);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(popView);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        EditText etNum = popView.findViewById(R.id.etCoinNum);
        TextView tvCoinNum = popView.findViewById(R.id.exchangeCoinNum);

        TextView tvGoldNum = popView.findViewById(R.id.tvCoinNum);
        TextView tvLockCoinNum = popView.findViewById(R.id.tvLockCoinNum);
        TextView tvCscNum = popView.findViewById(R.id.tvCscNum);

        tvGoldNum.setText(user_score);
        tvLockCoinNum.setText(lock_score);
        tvCscNum.setText(ad_balance);
        //增加
        popView.findViewById(R.id.ivAddNum).setOnClickListener(v -> {
            mNum++;
            etNum.setText(mNum+"");
            tvCoinNum.setText(mNum*prp+"");
        });
        //减少
        popView.findViewById(R.id.ivSub).setOnClickListener(v -> {
            if (mNum==1){
                ToastUtil.showToast("最小兑换数量为1");
                return;
            }
            mNum--;
            etNum.setText(mNum+"");
            tvCoinNum.setText(mNum*prp+"");
        });


        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etNum.getText().toString().matches("^0")) {//判断当前的输入第一个数是不是为0
                    etNum.setText("");
                }else {
                    try {
                        mNum = Integer.valueOf(s.toString());
                        tvCoinNum.setText(mNum*prp+"");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        popView.findViewById(R.id.ivExchangeCoin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exchange(mNum);
                popupWindow.dismiss();
            }
        });


        //关闭
        popView.findViewById(R.id.ivClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.setOnDismissListener(() -> onDisMiss());

    }


    /**
     * 兑换
     */
    public abstract void exchange(int num);

    /**
     * 消失
     */
    public abstract void onDisMiss();

    public void dissMiss(){
        popupWindow.dismiss();
    }

    public boolean getType(){
        if (popupWindow.isShowing()){
            return true;
        }else {
            return false;
        }
    }
}
